package com.lmaye.eshop.inventory.service.impl;

import com.lmaye.eshop.inventory.dao.RedisDao;
import com.lmaye.eshop.inventory.entity.ProductInventory;
import com.lmaye.eshop.inventory.mapper.ProductInventoryMapper;
import com.lmaye.eshop.inventory.service.ProductInventoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 商品库存 服务实现类
 * </p>
 *
 * @author Lottery
 * @since 2019-03-19
 */
@Slf4j
@Service
public class ProductInventoryServiceImpl extends ServiceImpl<ProductInventoryMapper, ProductInventory> implements ProductInventoryService {
    /**
     * redis缓存
     */
    @Autowired
    private RedisDao redisDao;

    /**
     * 更新商品库存
     *
     * @param productInventory 商品库存
     */
    @Override
    public void updateProductInventory(ProductInventory productInventory) {
        super.baseMapper.updateById(productInventory);
        log.info("************** 演示: 已更新数据库中的商品库存 - 商品id [{}], 商品库存 [{}]", productInventory.getProductId(), productInventory.getInventoryCount());
    }

    /**
     * 删除商品库存缓存
     *
     * @param productInventory 商品库存
     */
    @Override
    public void removeProductInventoryCache(ProductInventory productInventory) {
        redisDao.del("product:inventory:" + productInventory.getProductId());
        log.info("************** 演示: 已删除redis缓存中的商品库存 - 商品id [{}], 商品库存 [{}], key [{}]",
                productInventory.getProductId(), productInventory.getInventoryCount(), "product:inventory:" + productInventory.getProductId());
    }

    /**
     * 查询商品库存
     * - 根据商品ID
     *
     * @param productId 商品ID
     * @return ProductInventory
     */
    @Override
    public ProductInventory selectProductInventoryByID(Integer productId) {
        return super.baseMapper.selectById(productId);
    }

    /**
     * 设置商品库存缓存
     *
     * @param productInventory 商品库存
     */
    @Override
    public void setProductInventoryCache(ProductInventory productInventory) {
        redisDao.set("product:inventory:" + productInventory.getProductId(), String.valueOf(productInventory.getInventoryCount()));
        log.info("************** 演示: 已添加redis缓存中的商品库存 - 商品id [{}], 商品库存 [{}], key [{}]",
                productInventory.getProductId(), productInventory.getInventoryCount(), "product:inventory:" + productInventory.getProductId());
    }

    /**
     * 获取商品库存缓存
     * - 根据商品ID
     *
     * @param productId 商品ID
     * @return  ProductInventory
     */
    @Override
    public ProductInventory getProductInventoryCache(Integer productId) {
        try {
            String result = redisDao.get("product:inventory:" + productId);
            if(!StringUtils.isBlank(result)) {
                ProductInventory productInventory = new ProductInventory();
                productInventory.setProductId(productId);
                productInventory.setInventoryCount(Long.valueOf(result));
                return productInventory;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
